using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entities;
using System.Configuration;
namespace DAL
{
    public class tbDoanhNghiepDAL
    {
        private SqlConnection cn = null;
        public tbDoanhNghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbDoanhNghiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDoanhNghiep ", dn.TenDoanhNghiep);
            cm.Parameters.AddWithValue("@DiaChi", dn.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", dn.DienThoai);
            cm.Parameters.AddWithValue("@Email", dn.Email);
            cm.ExecuteNonQuery();

        }
        public void Update(tbDoanhNghiep dn)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDoanhNghiep ", dn.TenDoanhNghiep);
            cm.Parameters.AddWithValue("@DiaChi", dn.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", dn.DienThoai);
            cm.Parameters.AddWithValue("@Email", dn.Email);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDDoanhNghiep ", Id);
            cm.ExecuteNonQuery();
        }
        public tbDoanhNghiep.tbDoanhNghiepCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("DoanhNghiep_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbDoanhNghiep.tbDoanhNghiepCollection dncollection = new tbDoanhNghiep.tbDoanhNghiepCollection();
            while (dr.Read())
            {
                tbDoanhNghiep dn = new tbDoanhNghiep();
                dn.IDDoanhNghiep = int.Parse(dr.GetValue(0).ToString());
                dn.TenDoanhNghiep = dr.GetValue(1).ToString();
                dn.DiaChi = dr.GetValue(2).ToString();
                dn.DienThoai =int.Parse ( dr.GetValue(3).ToString());
                dn.Email = dr.GetValue(4).ToString();
                dncollection.Add(dn);              
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dncollection ;
        }
    }
}
